package com.mxx.qh.wxapi;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.mxx.qh.WXAuthorizeBroadCast;
import com.mxx.qh.WXPayResultBroadCast;
import com.mxx.qh.config.ConstantsConfig;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, ConstantsConfig.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onResp(BaseResp resp) {
        int type = resp.getType();
        Intent mIntent = new Intent();
        mIntent.setAction(WXPayResultBroadCast.WX_AUTHORIZE_ACTION);
        if (type == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Bundle mBundle = new Bundle();
            mBundle.putSerializable(ConstantsConfig.TYPE, type);
            mBundle.putSerializable(ConstantsConfig.CODE, resp.errCode);
            mIntent.putExtras(mBundle);
        }
        sendBroadcast(mIntent);
        finish();
    }
}